package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.business.rule.BusinessRuleInfoService;
import com.xbongbong.paas.pojo.dto.InitFundMenuDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.activity.pojo.vo.ActivityGetVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.form.pojo.dto.DeleteBusinessRuleInfoDTO;
import com.xbongbong.pro.form.pojo.dto.EnableBusinessRuleInfoDTO;
import com.xbongbong.pro.form.pojo.dto.FormUpdateBusinessRuleDTO;
import com.xbongbong.pro.form.pojo.dto.SortBusinessRuleInfoDTO;
import com.xbongbong.pro.form.pojo.vo.DeleteBusinessRuleInfoVO;
import com.xbongbong.pro.form.pojo.vo.EnableBusinessRuleInfoVO;
import com.xbongbong.pro.form.pojo.vo.FormUpdateBusinessRuleVO;
import com.xbongbong.pro.form.pojo.vo.SortBusinessRuleInfoVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/6/9 21:30
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/businessRuleInfo")
public class BusinessRuleInfoController {

    @Resource
    private BusinessRuleInfoService businessRuleInfoService;

    @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String set(@RequestBody @Valid InitFundMenuDTO initFundMenuDTO, BindingResult br) {
        XbbResponse<ActivityGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                businessRuleInfoService.setBusinessRule(initFundMenuDTO.getCorpidList());
                response = new XbbResponse<>();
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid FormUpdateBusinessRuleDTO formUpdateBusinessRuleDTO, BindingResult br) {
        XbbResponse<FormUpdateBusinessRuleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormUpdateBusinessRuleVO formUpdateBusinessRuleVO = businessRuleInfoService.save(formUpdateBusinessRuleDTO);
                response = new XbbResponse<>(formUpdateBusinessRuleVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid DeleteBusinessRuleInfoDTO deleteBusinessRuleInfoDTO, BindingResult br) {
        XbbResponse<DeleteBusinessRuleInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DeleteBusinessRuleInfoVO businessRuleInfoVO = businessRuleInfoService.delete(deleteBusinessRuleInfoDTO);
                response = new XbbResponse<>(businessRuleInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sort(@RequestBody @Valid SortBusinessRuleInfoDTO sortBusinessRuleInfoDTO, BindingResult br) {
        XbbResponse<SortBusinessRuleInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SortBusinessRuleInfoVO businessRuleInfoVO = businessRuleInfoService.sort(sortBusinessRuleInfoDTO);
                response = new XbbResponse<>(businessRuleInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid EnableBusinessRuleInfoDTO sortBusinessRuleInfoDTO, BindingResult br) {
        XbbResponse<EnableBusinessRuleInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EnableBusinessRuleInfoVO businessRuleInfoVO = businessRuleInfoService.enable(sortBusinessRuleInfoDTO);
                response = new XbbResponse<>(businessRuleInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
